package thread;

/**
 * Created by Administrator on 2017/10/18 0018.
 */
public class DeadLock2
{
    public static void main(String[] args)
    {
        Object obj1 = new Object();
        Object obj2 = new Object();
        new D("线程1", obj1, obj2).start();
        new D("线程2", obj2, obj1).start();
    }

    static class D extends Thread
    {
        Object obj1, obj2;
        public D(String name, Object obj1, Object obj2)
        {
            super(name);
            this.obj1 = obj1;
            this.obj2 = obj2;
        }

        @Override
        public void run()
        {
            synchronized (obj1)
            {
                try
                {
                    System.out.println(Thread.currentThread().getName() + "进来了");
                    Thread.sleep(100);
                    synchronized (obj2){}
                } catch (InterruptedException e)
                {
                    e.printStackTrace();
                }

            }
            System.out.println(Thread.currentThread().getName() + "出去了");
        }
    }
}
